Passed
Push — master ( 75c06e...4206c8 )
by Night
01:08
created

stringFuncs.restrictToCharRange   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 18
rs 9.3333
1
2
var stringFuncs = {
3
	
4
	// char type
5
	isOfCharRange: function(type){
6
		var char = this;
7
		
8
		// needs to be exactly 1 char!
9
		if (char.length != 1) {
1 ignored issue
show
Best Practice introduced by
Comparing char.length to 1 using the != operator is not safe. Consider using !== instead.
Loading history...
10
			return false;
11
		}
12
		
13
		// check type
14
		if (type == charCharRange.digit) {
0 ignored issues
show
Bug introduced by
The variable charCharRange seems to be never declared. If this is a global, consider adding a /** global: charCharRange */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
			var code = char.charCodeAt(0);
16
			return code >= 48 && code <= 57;
17
		}
18
		if (type == charCharRange.letter) {
19
			var code = char.charCodeAt(0);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable code already seems to be declared on line 15. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
20
			if (code >= 65 && code <= 90){
21
				return true;
22
			}
23
			if (code >= 97 && code <= 122){
24
				return true;
25
			}
26
		}
27
		if (type == charCharRange.space) {
28
			return char == " " || char == "\t" || char == "\r" || char == "\n";
29
		}
30
		if (type == charCharRange.symbol) {
31
			return symbolsCommon.indexOf(char) > -1;
0 ignored issues
show
Bug introduced by
The variable symbolsCommon seems to be never declared. If this is a global, consider adding a /** global: symbolsCommon */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
		}
33
		
34
		return false;
35
	},
36
	isNoneOfCharRange: function(type){
37
		var str = this;
38
		return !str.containsCharRange(type);
39
	},
40
	isAllOfCharRange: function(type, ifNoChars = false){
41
		var str = this;
42
		
43
		// exit quickly if blank string
44
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
45
			return ifNoChars;
46
		}
47
		
48
		// find char of type
49
		for (var c = 0, cl = str.length;c<cl;c++){
50
			if (!str.charAt(c).isOfCharRange(type)) {
51
				return false;
52
			}
53
		}
54
		return true;
55
	},
56 View Code Duplication
	isAllOfCharRange2: function(type1, type2, ifNoChars = false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
57
		var str = this;
58
		
59
		// exit quickly if blank string
60
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
61
			return ifNoChars;
62
		}
63
		
64
		// find char of type
65
		for (var c = 0, cl = str.length;c<cl;c++){
66
			if (!(str.charAt(c).isOfCharRange(type1) || str.charAt(c).isOfCharRange(type2))) {
67
				return false;
68
			}
69
		}
70
		return true;
71
	},
72
	containsCharRange: function(type, startAt = 0, ifNoChars = false){
73
		var str = this;
74
		
75
		// exit quickly if blank string
76
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
77
			return ifNoChars;
78
		}
79
		
80
		// find char of type
81
		for (var c = startAt, cl = str.length;c<cl;c++){
82
			if (str.charAt(c).isOfCharRange(type)) {
83
				return true;
84
			}
85
		}
86
		return false;
87
	},
88
	startsWithCharRange: function(type, ifNoChars = false){
89
		var str = this;
90
		
91
		// exit quickly if blank string
92
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
93
			return ifNoChars;
94
		}
95
		
96
		// check if begins with char of type
97
		return str.charAt(0).isOfCharRange(type);
98
	},
99
	endsWithCharRange: function(type, ifNoChars = false){
100
		var str = this;
101
		
102
		// exit quickly if blank string
103
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
104
			return ifNoChars;
105
		}
106
		
107
		// check if ends with char of type
108
		return str.charAt(str.length - 1).isOfCharRange(type);
109
	},
110 View Code Duplication
	indexOfCharRange: function(type, startAt = null, forwards = true){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
111
		var str = this;
112
		
113
		// exit quickly if blank string
114
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
115
			return -1;
116
		}
117
		
118
		// find char of type
119
		if (forwards) {
120
			if (startAt == null) {
1 ignored issue
show
Best Practice introduced by
Comparing startAt to null using the == operator is not safe. Consider using === instead.
Loading history...
121
				startAt = 0;
122
			}
123
			for (var c = startAt, cl = str.length;c<cl;c++){
124
				if (str.charAt(c).isOfCharRange(type)) {
125
					return c;
126
				}
127
			}
128
		}else {
129
			if (startAt == null) {
130
				startAt = str.length - 1;
131
			}
132
			for (var c = startAt;c >= 0;c--){
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable c already seems to be declared on line 123. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
133
				if (str.charAt(c).isOfCharRange(type)) {
134
					return c;
135
				}
136
			}
137
		}
138
		return -1;
139
	},
140
	lastIndexOfCharRange: function(type){
141
		var str = this;
142
		return str.indexOfCharRange(type, null, false);
143
	},
144
	restrictToCharRange: function(type, startAt = 0){
145
		var str = this;
146
		
147
		// exit quickly if blank string
148
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
149
			return "";
150
		}
151
		
152
		// extract all chars of given type
153
		var output = [];
154
		for (var c = startAt, cl = str.length;c<cl;c++){
155
			var char = str.charAt(c);
156
			if (char.isOfCharRange(type)) {
157
				output.push(char);
158
			}
159
		}
160
		return output.join("");
161
	},
162 View Code Duplication
	afterCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
163
		var text = this;
164
		if (text == null) { return returnAll ? text : ''; }
1 ignored issue
show
Best Practice introduced by
Comparing text to null using the == operator is not safe. Consider using === instead.
Loading history...
165
		if (!forward) {
166
			var idx = S.LastIndexOfCharRange(text, type);
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
167
		}else {
168
			idx = S.IndexOfCharRange(text, type, startAt);
169
		}
170
		if (idx == -1) { return returnAll ? text : ''; }
171
		if (!inclusive) {
172
			idx++;
173
		}
174
		return text.substr(idx);
175
	},
176 View Code Duplication
	beforeCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
177
		var text = this;
178
		if (text == null) { return returnAll ? text : ''; }
1 ignored issue
show
Best Practice introduced by
Comparing text to null using the == operator is not safe. Consider using === instead.
Loading history...
179
		if (forward) {
180
			var idx = S.IndexOfCharRange(text, type, startAt);
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
181
		}else {
182
			idx = S.LastIndexOfCharRange(text, type);
183
		}
184
		if (idx == -1) { return returnAll ? text : ''; }
185
		if (!inclusive) {
186
			idx--;
187
		}
188
		return text.substr(0, idx);
189
	},
190
	
191
	// digits
192
	removeDigits: function(){
193
		var str = this;
194
		
195
		// exit quickly if blank string
196
		if (str == null || str.length == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing str to null using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
197
			return "";
198
		}
199
		
200
		// extract all chars that are not digits
201
		var output = [];
202
		for (var c = 0, cl = str.length;c<cl;c++){
203
			var char = str.charAt(c);
204
			if (!S.IsDigit(char)) {
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
205
				output.push(char);
206
			}
207
		}
208
		return output.join("");
209
	},
210
	
211
	none:null
212
};
213
214
// register funcs
215
UB.registerFuncs(String.prototype, stringFuncs);
0 ignored issues
show
Bug introduced by
The variable UB seems to be never declared. If this is a global, consider adding a /** global: UB */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...